package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.web.exception.CustomException;
import com.etoak.common.web.utils.PageUtil;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * IngredientsService服务实现类
 * </p>
 *
 * @author etoak
 * @since 2025-03-04
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {
    @Override
    public Ingredients getByName(String name) {
        /**
         * select * from t_ingredients where name = #{name}
         */
        return lambdaQuery().eq(Ingredients::getName,name).one();
    }

    @Override
    public void add(Ingredients ingredients) {
        if(ObjectUtils.isNotEmpty(getByName(ingredients.getName()))){
            throw new CustomException("食材名称重复！");
        }

        /**
         * 设置创建时间
         */
        ingredients.setCreateTime(DateUtil.now());
        /**
         * 保存食材信息
         */
        this.save(ingredients);
    }

    @Override
    public PageVO<Ingredients> list(Ingredients ingredients) {
        // 开始分页查询
       PageUtil.startPage();
        // 生成符合条件的食材列表，并按创建时间降序排列
        List<Ingredients> ingredientsList = getIngredientsList(ingredients);
        // 将查询结果封装成PageInfo对象
        PageInfo<Ingredients> pageInfo = new PageInfo<>(ingredientsList);
        // 返回包含总记录数和食材列表的PageVO对象
        return new PageVO<>(pageInfo.getTotal(),ingredientsList);

    }

    private List<Ingredients> getIngredientsList(Ingredients ingredients) {
        return lambdaQuery()
                .likeRight(StringUtils.isNotBlank(ingredients.getName()), Ingredients::getName, ingredients.getName())
                .eq(StringUtils.isNotBlank(ingredients.getType()), Ingredients::getType, ingredients.getType())
                .orderByDesc(Ingredients::getCreateTime)
                .list();
    }

    @Override
    public void update(int id, Ingredients ingredients) {
        if(ObjectUtils.isEmpty(getById(id))){
            throw new CustomException("食材不存在！");
        }
        /**
         * 食材名称
         */
        String name = ingredients.getName();
        if(StringUtils.isNotBlank(name)){
            //根据食材名称查询食材信息
            Ingredients savedIngredients = getByName(name);
            //查询到了食材并且要和更新的食材id不一致 认为名臣个重复
            if(ObjectUtils.isNotEmpty(savedIngredients)&&!savedIngredients.getId().equals(id)){
                throw new CustomException("食材名称不能重复！");
            }
        }
        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);
    }

    @Override
    public void delete(int id) {
        if(ObjectUtils.isEmpty(getById(id))){
            throw new CustomException("食材不存在！");
        }
        //食材id存在，判断食材有没有被菜品使用

        //如果没有使用则调用
        this.removeById(id);
    }

    @Override
    public List<Ingredients> listByType(String type) {
        Ingredients ingredients=new Ingredients();
        ingredients.setType(type);
        List<Ingredients> ingredientsList=getIngredientsList(ingredients);
        return ingredientsList;
    }
}
